package com.hnkywl.sts.action.portal;

import com.google.gson.Gson;
import com.hnkywl.sts.listener.OnlineInfo;
import com.hnkywl.sts.listener.UserInfo;
import com.hnkywl.sts.service.portal.LoginService;
import com.hnkywl.sts.service.sys.YgService;
import com.hnkywl.sts.service.xtgl.DlrzService;
import com.hnkywl.sts.util.CookieUtils;
import net.ruixin.web.struts2.SimpleActionSupport;
import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Created with IntelliJ IDEA.
 * User: jerry
 * Date: 13-3-13
 * Time: 上午11:17
 * To change this template use File | Settings | File Templates.
 */
@ParentPackage("default")
@Namespace("/portal")
public class LoginAction extends SimpleActionSupport {
    private String userName;

    private String password;

    private String xx;

    private String oo;

    private String idenCode;

    private String yztCode;

    private String message;

    private String remoteAddr;

    private CookieUtils cookieUtils = new CookieUtils();

    private LoginService loginService;

    private YgService ygService;

    @Action("choiceLogin")
    public String choiceLogin() {
        remoteAddr = this.request.getRemoteAddr();
        if (remoteAddr.equals("0:0:0:0:0:0:0:1"))
            remoteAddr = "127.0.0.1";
        return SUCCESS;
    }


    @Actions({@Action("login")})
    public String login() {
        if (this.isPost()) {
            if (StringUtils.isNotEmpty(userName) && StringUtils.isNotEmpty(password)) {
                String rand = (String) this.session.get("GYLWL_RAND_SESSION_KEY");
                if (idenCode.equals(rand)) {
                    Map yg = this.loginService.login(userName, password);
                    if (yg != null) {
                        if ((Boolean) yg.get("kdl")) {
                            Set<Long> roleList = initUserRole((Long) yg.get("id"));
                            YgService.setCurrentUser(yg, roleList);
                            // if (cookieUtils.getCookie(this.request) == null) {
                            Cookie cookie = cookieUtils.addCookie(userName);
                            response.addCookie(cookie);
                            //}
                            this.renderText("true");
                        } else
                            this.renderText("用户被禁用，无法登录！");
                        return null;

                    } else {
                        this.renderText("用户名或者密码输入错误！");
                        return null;
                    }
                } else {
                    this.renderText("验证码输入错误！");
                    return null;
                }
            }
            this.renderText("用户名或者密码不能为空！");
            return null;
        } else {
            userName = cookieUtils.getCookie(this.request);
            return SUCCESS;
        }
    }

    @Actions({@Action("yzt")})
    public void yzt() {
        if (StringUtils.isNotEmpty(xx) && StringUtils.isNotEmpty(oo)) {
            Map yg = this.loginService.loginYzt(xx, oo);
            if (yg != null) {
                if ((Boolean) yg.get("kdl")) {
                    Set<Long> roleList = initUserRole((Long) yg.get("id"));
                    YgService.setCurrentUser(yg, roleList);
                    this.renderText("true");
                } else
                    this.renderText("用户被禁用，无法登录！");
            } else {
                this.renderText("用户名或者密码输入错误！");
            }
        } else
            this.renderText("用户名或者密码不能为空！");
    }

    @Actions({@Action("ajaxLogin")})
    public String yztLogin() {
        if (this.isPost()) {
            if (StringUtils.isNotEmpty(xx) && StringUtils.isNotEmpty(oo)) {
                Map yg = this.loginService.loginYzt(xx, oo);
                if (yg != null) {
                    if ((Boolean) yg.get("kdl")) {
                        Set<Long> roleList = initUserRole((Long) yg.get("id"));
                        YgService.setCurrentUser(yg, roleList);
                        this.redirect("../main/index");
                        return null;
                    } else
                        this.setMessage("用户被禁用，无法登录！");
                } else {
                    this.setMessage("用户名或者密码输入错误！");
                }
            } else
                this.setMessage("用户名或者密码不能为空！");
        }
        return SUCCESS;
    }

    private Set<Long> initUserRole(Long id) {
        List list = ygService.findYgAllJs(id);
        Set<Long> jsSet = new HashSet<Long>();
        jsSet.addAll(list);
        return jsSet;
    }

    //系统心跳
    @Action("keepAlive")
    public void keepAlive() {
        renderText(String.valueOf(OnlineInfo.getUserCount()));
    }





    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getIdenCode() {
        return idenCode;
    }

    public void setIdenCode(String idenCode) {
        this.idenCode = idenCode;
    }

    public String getYztCode() {
        return yztCode;
    }

    public void setYztCode(String yztCode) {
        this.yztCode = yztCode;
    }

    public String getRemoteAddr() {
        return remoteAddr;
    }

    public void setRemoteAddr(String remoteAddr) {
        this.remoteAddr = remoteAddr;
    }

    public String getXx() {
        return xx;
    }

    public void setXx(String xx) {
        this.xx = xx;
    }

    public String getOo() {
        return oo;
    }

    public void setOo(String oo) {
        this.oo = oo;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    @Autowired
    public void setLoginService(LoginService loginService) {
        this.loginService = loginService;
    }

    @Autowired
    public void setYgService(YgService ygService) {
        this.ygService = ygService;
    }

}
